<?php 
namespace app\common\controller;

use app\common\model\cash\CashList;
use app\common\model\user\Users;
use app\common\model\admin\AdminParam;

class Cash
{
	/**
	 * 提币列表
	 * @param array $where 查询条件
	 * @return array
	 */
	public function cashList($where = [])
	{
		$list = CashList::where($where)->order('id desc')->paginate() -> toArray();
		foreach ($list as &$rows) {
			$rows['user'] = Users::where(['id' => $rows['user_id']])->field('id,user_name,mobile')->find();
		}
		return $list;
	}
	
	/**
	 * 提币
	 * @param int $user_id 提币用户ID
	 * @param double $num 提币数量
	 * @param int $coin 提币币种
	 * @param string $address 提币地址
	 * @return boolean
	 */
	public function addCash($user_id,$num,$coin,$address)
	{
		$user = Users::get($user_id);
		//获取用户的参数
		$param = Param::getParam($user_id);
		//获取提币手续费比例
		$fee_param = $param['cash_fee'] / 100;
		//计算实际到账
		$ready_num = $num * (1 - $fee_param);
		//计算手续费
		$fee_num = $num * $fee_param;
		//计算账户余额
		$surplus_num = 0;
		//组装数据
		$data = [
				'user_id' => $user_id,
				'coin' => $coin,
				'num' => $num,
				'ready_num' => $ready_num,
				'fee' => $fee_num,
				'surplus_num' => $surplus_num,
				'address' => $address,
				'state' => 0,
				'create_time' => time(),
				'update_time' => time()
		];
		$res = CashList::create($data);
		return $res ? true : false;
	}
}